package cn.lingyangwl.agile.food.core.service.impl;

import cn.lingyangwl.agile.food.core.assembly.StoreTagAssembly;
import cn.lingyangwl.agile.food.core.mapper.StoreTagMapper;
import cn.lingyangwl.agile.food.core.service.StoreTagService;
import cn.lingyangwl.agile.food.model.entity.ShopStoreTag;
import cn.lingyangwl.agile.food.model.rqrs.store.StoreTagListReq;
import cn.lingyangwl.agile.food.model.rqrs.store.StoreTagResp;
import cn.lingyangwl.agile.food.model.rqrs.store.StoreTagSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.store.StoreTagUpdateReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门店标签
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class StoreTagServiceImpl extends ServiceImpl<StoreTagMapper, ShopStoreTag>
    implements IService<ShopStoreTag>, StoreTagService {

    @Resource
    private StoreTagAssembly storeTagAssembly;
    
    @Override
    public List<StoreTagResp> listStoreTag(StoreTagListReq req) {
        LambdaQueryWrapper<ShopStoreTag> lqw = ShopStoreTag.lqw()
                .like(StringUtils.isNotEmpty(req.getName()), ShopStoreTag::getName, req.getName())
                .orderByDesc(ShopStoreTag::getCreatedAt).orderByAsc(ShopStoreTag::getSortNo);
    	return this.list(lqw).stream()
    	        .map(storeTagAssembly::toStoreTagResp).collect(Collectors.toList());
    }

    @Override
    public StoreTagResp getStoreTag(Long id) {
        ShopStoreTag entity = this.getById(id);
    	return storeTagAssembly.toStoreTagResp(entity);
    }

    @Override
    public boolean saveStoreTag(StoreTagSaveReq req) {
        ShopStoreTag entity = storeTagAssembly.toStoreTag(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateStoreTag(StoreTagUpdateReq req) {
        ShopStoreTag entity = storeTagAssembly.toStoreTag(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteStoreTag(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteStoreTag(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
